@echo off
setlocal enabledelayedexpansion

:: =============== 配置区 ===============
set SERVICE_NAME=ReportCaptureRobot
set PROJECT_ROOT=C:\App\capture-robot
:: 已配置环境路径
set PYTHON_PATH="python"
set GIT_REPO=https://gitee.com/LShawn/capture-robot.git
set LOG_DIR=%PROJECT_ROOT%\logs
set NSSM_VERSION=2.24
:: set CHROME_URL=https://dl.google.com/chrome/install/latest/chrome_installer.exe
:: ======================================

:init
if "%1"=="/install" goto :install
if "%1"=="/update" goto :update
if "%1"=="/uninstall" goto :uninstall

echo 使用方法:
echo   deploy.bat /install    - 全新安装服务
echo   deploy.bat /update     - 更新应用程序
echo   deploy.bat /uninstall  - 完全卸载
goto :eof

:install
echo ██ 开始安装报告截图机器人服务 ██

:: 检查管理员权限
@REM net session >nul 2>&1
@REM if %errorLevel% neq 0 (
@REM     echo 需要以管理员身份运行
@REM     pause
@REM     exit /b
@REM )

:: 创建项目目录
if not exist "%PROJECT_ROOT%" (
    mkdir "%PROJECT_ROOT%"
)

:: 安装系统组件
echo 正在安装系统组件...
powershell -Command "Install-WindowsFeature Web-Server" >nul
powershell -Command "Install-WindowsFeature NET-Framework-45-Core" >nul


:: 克隆代码仓库
if not exist "%PROJECT_ROOT%\.git" (
    echo 正在克隆代码仓库...
    git clone %GIT_REPO% "%PROJECT_ROOT%"
)

:: 创建Python虚拟环境
if not exist "%PROJECT_ROOT%\venv" (
    echo 正在创建Python虚拟环境...
    %PYTHON_PATH% -m venv "%PROJECT_ROOT%\venv"
)

:: 创建服务日志目录
if not exist "%LOG_DIR%" (
    mkdir "%LOG_DIR%"
)

:: 安装Python依赖
call "%PROJECT_ROOT%\venv\Scripts\activate.bat"
pip install -r "%PROJECT_ROOT%\requirements.txt"

:: 配置NSSM服务
:: 已提前安装nssm2.24
:: 使用短路径避免文件夹空格问题
set NSSM_PATH="C:\Progra~1\nssm\nssm-2.24\win64\nssm.exe"
echo 正在配置Windows服务...
"%NSSM_PATH%" install %SERVICE_NAME% ^
    "%PROJECT_ROOT%\venv\Scripts\python.exe" ^
    "%PROJECT_ROOT%\main.py"
:: 配置windows服务参数
"%NSSM_PATH%" set %SERVICE_NAME% AppDirectory "%PROJECT_ROOT%"
"%NSSM_PATH%" set %SERVICE_NAME% AppStdout "%LOG_DIR%\service.log"
"%NSSM_PATH%" set %SERVICE_NAME% AppStderr "%LOG_DIR%\error.log"
"%NSSM_PATH%" set %SERVICE_NAME% AppRotateFiles 1
"%NSSM_PATH%" set %SERVICE_NAME% AppRotateOnline 1
"%NSSM_PATH%" set %SERVICE_NAME% AppRotateSeconds 86400
"%NSSM_PATH%" set %SERVICE_NAME% AppRestartDelay 5000
"%NSSM_PATH%" set %SERVICE_NAME% DependOnService EventLog
:: 设置服务环境变量
"%NSSM_PATH%" set %SERVICE_NAME% AppEnvironmentExtra "PATH=%PATH%;C:\Users\Administrator\AppData\Local\Programs\Python\Python313\Scripts"

:: 配置文件权限
icacls "%PROJECT_ROOT%" /grant:r "NETWORK SERVICE:(OI)(CI)RX"
icacls "%PROJECT_ROOT%\logs" /grant "NETWORK SERVICE:(OI)(CI)RW"

:: 启动服务
net start %SERVICE_NAME%

echo 安装完成！服务状态：
sc query %SERVICE_NAME%
goto :eof

:update
echo ██ 开始更新应用程序 ██

:: 停止服务
net stop %SERVICE_NAME%

:: 更新代码
cd "%APP_ROOT%"
git pull origin main
call "%PROJECT_ROOT%\venv\Scripts\activate.bat"
pip install -r requirements.txt --upgrade

:: 清理旧文件
del /q "%LOG_DIR%\*.log"

:: 启动服务
net start %SERVICE_NAME%
echo 更新完成！
goto :eof

:uninstall
echo ██ 卸载服务 ██
net stop %SERVICE_NAME%
"%NSSM_PATH%" remove %SERVICE_NAME% confirm
rd /s /q "%PROJECT_ROOT%"
echo 已完全卸载